package com.zqwh.storemonitor.activity;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

import com.zqwh.storemonitor.R;
import com.zqwh.storemonitor.domain.Camera;

import java.util.List;

import static com.zqwh.storemonitor.Config.CAMERA_SPAN_COUNT;

public class CameraAdapter extends RecyclerView.Adapter<CameraAdapter.LocalViewHolder> {
    private Context context;
    private List<Camera> cameraList;
    private CameraAdapter.OnItemClickListener onItemClickListener;
    private CameraAdapter.OnItemRefreshListener onItemRefreshListener;
    private int firstItemIndex = 0;
    private int lastItemIndex = 0;

    public CameraAdapter(Context context, List<Camera> cameraList) {
        this.context = context;
        this.cameraList = cameraList;
    }

    /**
     * 定义RecyclerView选项单击事件的回调接口
     */
    public interface OnItemClickListener{
        void onItemClick(CameraAdapter.LocalViewHolder holder, int position, Camera camera);
        void onDoubleItemClick(CameraAdapter.LocalViewHolder holder, int position, Camera camera);
    }

    public void setOnItemClickListener(CameraAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemRefreshListener{
        void onItemAppear(CameraAdapter.LocalViewHolder holder, int position, Camera camera);
    }

    public void setOnItemRefreshListener(OnItemRefreshListener onItemRefreshListener){
        this.onItemRefreshListener = onItemRefreshListener;
    }

    @NonNull
    @Override
    public CameraAdapter.LocalViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        int resource = R.layout.item_camera_1;
        if (CAMERA_SPAN_COUNT == 1){
            resource = R.layout.item_camera_1;
        }else if (CAMERA_SPAN_COUNT == 2){
            resource = R.layout.item_camera_2;
        }
        View itemView = LayoutInflater.from(context).inflate(resource, viewGroup, false);
        return new CameraAdapter.LocalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final CameraAdapter.LocalViewHolder holder, final int position) {
        final Camera camera = cameraList.get(position);
        if (camera.getMonitorSv() == null) {
            camera.setMonitorSv(holder.surfaceView);
        }
        if (camera.isSelected()){
            holder.viewSelected.setVisibility(View.VISIBLE);
        }else {
            holder.viewSelected.setVisibility(View.INVISIBLE);
        }

        if (onItemClickListener != null){
            holder.surfaceView.setOnClickListener(new MyClickListener() {
                @Override
                public void onSingleClick(View v) {
                    onItemClickListener.onItemClick(holder, position, camera);
                }

                @Override
                public void onDoubleClick(View v) {
                    onItemClickListener.onDoubleItemClick(holder, position, camera);
                }
            });
        }

        if (onItemRefreshListener != null){
            onItemRefreshListener.onItemAppear(holder, position, camera);
        }
    }

    @Override
    public int getItemCount() {
        return cameraList.size();
    }

    static class LocalViewHolder extends RecyclerView.ViewHolder{
        SurfaceView surfaceView;
        View viewSelected;

        public LocalViewHolder(@NonNull View itemView) {
            super(itemView);
            surfaceView = itemView.findViewById(R.id.sv_realplay);
            viewSelected = itemView.findViewById(R.id.view_selected);
        }
    }
}
